package com.neuedu.mblog.service;

import com.neuedu.mblog.dao.ChannelDao;
import com.neuedu.mblog.entiry.Channel;

import javax.servlet.http.HttpServletRequest;
import java.sql.SQLException;
import java.util.List;

/**
 * 项目：mblog_java4
 * 创建时间：  2021-12-29   9:21
 * 作者 :jshand
 * 描述 :
 */
public class ChannelService {
    private ChannelDao channelDao = new ChannelDao();


    /**
     * 查询集合不分页
     * @param request
     * @return
     */
    public List<Channel> selectList(HttpServletRequest request) throws SQLException {
        //
        String sql = " SELECT   id,   key_ ,  NAME ,   STATUS , thumbnail , weight FROM  mto_channel ";
        return channelDao.selectList(sql ,null);
    }


    /**
     * 修改前查询数据
     * @param request
     * @return
     * @throws SQLException
     */
    public Channel selectById(HttpServletRequest request) throws SQLException {
        String id = request.getParameter("id");
        return channelDao.selectById(Integer.parseInt(id));
    }

    public boolean update(HttpServletRequest request) throws SQLException {

        Channel channel = new Channel();

        channel.setId(Integer.parseInt(request.getParameter("id")));
        channel.setName(request.getParameter("name"));
        channel.setKey(request.getParameter("key"));
        channel.setStatus(Integer.parseInt(request.getParameter("status")));
        //还差一个缩略图

        return channelDao.update(channel) >0;
    }

    public boolean save(HttpServletRequest request) throws SQLException {
        Channel channel = new Channel();

//        channel.setId(Integer.parseInt(request.getParameter("id")));
        channel.setName(request.getParameter("name"));
        channel.setKey(request.getParameter("key"));
        channel.setStatus(Integer.parseInt(request.getParameter("status")));
        //还差一个缩略图

        return channelDao.save(channel) >0;
    }

    public boolean deleteById(HttpServletRequest request) throws SQLException {
        String id = request.getParameter("id");
        return channelDao.delete(Integer.parseInt(id))>0;
    }
}
